﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;

namespace Minecraft.ModInstaller
{
    public class InputWrangler
    {
        public InputWrangler(string InputPath, InputType Type)
        {
            if (Type == InputType.ZipFolder)
            {
                TimeSpan USpan = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long UTimeStamp = (long)Math.Floor(USpan.TotalSeconds);

                string TempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
                    "MinecraftTools\\Temp\\" + UTimeStamp.ToString());

                FastZip f = new FastZip();
                f.ExtractZip(InputPath, TempPath, "");

                InputPath = TempPath;
            }

            if (Type == InputType.File)
            {
                _GuessedContentType = ContentType.File;
                _ProbableRoot = InputPath;
            }
            else
            {
                //Try to guess the content type based on the contents of the folder
                _GuessedContentType = ContentType.Unknown;
                GuessContentTypeRecursive(InputPath);

                if (ProbableRoot == null)
                    _ProbableRoot = InputPath;
            }
        }

        private void GuessContentTypeRecursive(string InputPath)
        {
            foreach (string File in Directory.GetFiles(InputPath))
                {
                    if (File.EndsWith(".class", true, System.Globalization.CultureInfo.InvariantCulture))
                    {
                        _GuessedContentType = ContentType.Mod;
                        if(_ProbableRoot == null)
                            _ProbableRoot = InputPath;
                    }
                    if (File.EndsWith(".png", true, System.Globalization.CultureInfo.InvariantCulture) && _GuessedContentType == ContentType.Unknown)
                        _GuessedContentType = ContentType.Skin;

                    if (File.EndsWith("terrain.png", true, System.Globalization.CultureInfo.InvariantCulture) && _ProbableRoot == null)
                        _ProbableRoot = InputPath;
                }

            foreach (string Folder in Directory.GetDirectories(InputPath))
                GuessContentTypeRecursive(Folder);
        }

        private ContentType _GuessedContentType;
        public ContentType GuessedContentType
        {
            get
            {
                return _GuessedContentType;
            }
        }
        private string _ProbableRoot;
        public string ProbableRoot
        {
            get
            {
                return _ProbableRoot;
            }
        }

        public enum InputType
        {
            File,       //A single file
            Folder,     //A single folder
            ZipFolder   //A zip archive
        }

        public enum ContentType
        {
            Skin,       //A graphics pack
            Mod,        //A gameplay-altering mod
            File,       //A single loose file
            Unknown
        }
    }
}
